//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
namespace DotNetNuke.UI.WebControls
{
	[ToolboxData("<{0}:CollectionEditorControl runat=server></{0}:CollectionEditorControl>")]
	public class CollectionEditorControl : PropertyEditorControl
	{
		private string _CategoryDataField;
		private string _EditorDataField;
		private string _LengthDataField;
		private string _NameDataField;
		private string _RequiredDataField;
		private string _TypeDataField;
		private string _ValidationExpressionDataField;
		private string _ValueDataField;
		private string _VisibleDataField;
		private string _VisibilityDataField;
		private IEnumerable _UnderlyingDataSource;
		protected override IEnumerable UnderlyingDataSource {
			get {
				if (_UnderlyingDataSource == null) {
					_UnderlyingDataSource = (IEnumerable)DataSource;
				}
				return _UnderlyingDataSource;
			}
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the Category.")]
		public string CategoryDataField {
			get { return _CategoryDataField; }
			set { _CategoryDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the Editor Type.")]
		public string EditorDataField {
			get { return _EditorDataField; }
			set { _EditorDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that determines the length.")]
		public string LengthDataField {
			get { return _LengthDataField; }
			set { _LengthDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the Label's Text property.")]
		public string NameDataField {
			get { return _NameDataField; }
			set { _NameDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that determines whether an item is required.")]
		public string RequiredDataField {
			get { return _RequiredDataField; }
			set { _RequiredDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the EditControl's Type.")]
		public string TypeDataField {
			get { return _TypeDataField; }
			set { _TypeDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the EditControl's Expression Validator.")]
		public string ValidationExpressionDataField {
			get { return _ValidationExpressionDataField; }
			set { _ValidationExpressionDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the EditControl's Value property.")]
		public string ValueDataField {
			get { return _ValueDataField; }
			set { _ValueDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that determines whether the item is visble.")]
		public string VisibleDataField {
			get { return _VisibleDataField; }
			set { _VisibleDataField= value; }
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that determines the visibility.")]
		public string VisibilityDataField {
			get { return _VisibilityDataField; }
			set { _VisibilityDataField= value; }
		}
		protected override void AddEditorRow(ref Table tbl, object obj)
		{
			Hashtable fields = new Hashtable();
			fields.Add("Category", CategoryDataField);
			fields.Add("Editor", EditorDataField);
			fields.Add("Name", NameDataField);
			fields.Add("Required", RequiredDataField);
			fields.Add("Type", TypeDataField);
			fields.Add("ValidationExpression", ValidationExpressionDataField);
			fields.Add("Value", ValueDataField);
			fields.Add("Visibility", VisibilityDataField);
			fields.Add("Length", LengthDataField);
			AddEditorRow(ref tbl, NameDataField, new CollectionEditorInfoAdapter(obj, this.ID, NameDataField, fields));
		}
		protected override string GetCategory(object obj)
		{
			PropertyInfo objProperty;
			string _Category = Null.NullString;
			if (!String.IsNullOrEmpty(CategoryDataField)) {
				objProperty = obj.GetType().GetProperty(CategoryDataField);
				if (!(objProperty == null || (objProperty.GetValue(obj, null) == null))) {
					_Category = Convert.ToString(objProperty.GetValue(obj, null));
				}
			}
			return _Category;
		}
		protected override string[] GetGroups(IEnumerable arrObjects)
		{
			ArrayList arrGroups = new ArrayList();
			PropertyInfo objProperty;
			
			foreach (object obj in arrObjects) {
				if (!String.IsNullOrEmpty(CategoryDataField)) {
					objProperty = obj.GetType().GetProperty(CategoryDataField);
					if (!((objProperty == null) || (objProperty.GetValue(obj, null) == null))) {
						string _Category = Convert.ToString(objProperty.GetValue(obj, null));
						if (!arrGroups.Contains(_Category)) {
							arrGroups.Add(_Category);
						}
					}
				}
			}
			string[] strGroups = new string[arrGroups.Count];
			for (int i = 0; i <= arrGroups.Count - 1; i++) {
				strGroups[i] = Convert.ToString(arrGroups[i]);
			}
			return strGroups;
		}
		protected override bool GetRowVisibility(object obj)
		{
			bool isVisible = true;
			PropertyInfo objProperty;
			objProperty = obj.GetType().GetProperty(VisibleDataField);
			if (!(objProperty == null || (objProperty.GetValue(obj, null) == null))) {
				isVisible = Convert.ToBoolean(objProperty.GetValue(obj, null));
			}
			if (!isVisible && EditMode == PropertyEditorMode.Edit) {
				objProperty = obj.GetType().GetProperty(RequiredDataField);
				if (!(objProperty == null || (objProperty.GetValue(obj, null) == null))) {
					isVisible = Convert.ToBoolean(objProperty.GetValue(obj, null));
				}
			}
			return isVisible;
		}
	}
}
